package com.cyzy.config;


import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    private final Environment environment;
    public RedisConfig(Environment environment) {
        this.environment = environment;
    }

//    @Bean
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(redisConnectionFactory);
//
//        // 设置键的序列化方式
//        template.setKeySerializer(new JdkSerializationRedisSerializer());
//        template.setHashKeySerializer(new JdkSerializationRedisSerializer());
//
//        // 设置值的序列化方式（使用JSON序列化，方便存储对象）
//        template.setValueSerializer(new JdkSerializationRedisSerializer());
//        template.setHashValueSerializer(new JdkSerializationRedisSerializer());
//
//        template.afterPropertiesSet();
//        return template;
//    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // Key 使用字符串序列化
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());

        // Value 使用JSON序列化
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
    @Bean
    public RedissonClient redissonClient() {
        String redisUrl = environment.getProperty("redis.url", "redis://1.94.233.8:6379");
        String redisPassword = environment.getProperty("redis.password", "zhhzhh8888xmhxmh");
        int redisDatabase = environment.getProperty("redis.database", Integer.class, 0);

        Config config = new Config();
        config.useSingleServer()
                .setAddress(redisUrl)
                .setPassword(redisPassword)
                .setDatabase(redisDatabase)
                .setConnectionPoolSize(64)  // 连接池大小
                .setConnectionMinimumIdleSize(10)  // 最小空闲连接数
                .setConnectTimeout(5000)  // 连接超时时间
                .setTimeout(3000)  // 命令超时时间
                .setRetryAttempts(3)  // 重试次数
                .setRetryInterval(1500);  // 重试间隔

        return Redisson.create(config);
    }
}
